# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
'''
如果有环的话,所谓的遇见就是久别重逢
'''
class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        while head:
            if head.val == '$#%&&&&':
                return True
            else:
                head.val = '$#%&&&&'
            head = head.next
        return False

'''
快慢指针，注意起点错开就可以了，slow == fast return true就可以了
'''
class Solution:
    def hasCycle(self, head):
        if not head or not head.next:  # 节点不存在或下一个节点不存在
            return False

        slow = head
        fast = head.next  # 起点错开，防止下面循环判断有问题
        # nonetype没有next
        while fast and fast.next:  # 快节点和快节点下一个节点不为空
            if slow == fast:
                return True
            slow = slow.next
            fast = fast.next.next
        return False
